#!/bin/bash

# Copyright (c) 2025 Battelle Energy Alliance, LLC.  All rights reserved.

sed -i 's/^exit 0//' /etc/rc.local 2>/dev/null

cat << 'EOF' >> /etc/rc.local

# if this is the initial boot, format and set up storage drive(s)
if [ -f /etc/capture_storage_format.crypt ]; then
  CAPTURE_STORAGE_FORMAT_FILE="/etc/capture_storage_format.crypt"
  CAPTURE_STORAGE_FORMAT_FLAG="-c"
else
  CAPTURE_STORAGE_FORMAT_FILE="/etc/capture_storage_format"
  CAPTURE_STORAGE_FORMAT_FLAG=""
fi
if [ -f "$CAPTURE_STORAGE_FORMAT_FILE" ]; then
  logger "Initializing disk(s) to store captured artifacts"
  date >>/var/log/os-disk-config.log 2>&1
  python3 /usr/local/bin/os-disk-config.py -u $CAPTURE_STORAGE_FORMAT_FLAG >>/var/log/os-disk-config.log 2>&1
  rm -f "$CAPTURE_STORAGE_FORMAT_FILE"
fi

# other agg-specific initialization prior to starting up
echo "Running Malcolm initialization" > /dev/tty0
/usr/local/bin/agg-init.sh

systemctl mask ctrl-alt-del.target

if [ -x /usr/sbin/aideinit ] && [ ! -s /var/lib/aide/aide.db ]; then
    > /var/lib/aide/aide.db
    /usr/sbin/aideinit --yes --force
fi

exit 0
EOF

sed -i "1i #!/bin/sh" /etc/rc.local

chmod o+rx /etc/rc.local /usr/local/bin/*.sh /usr/local/bin/system-quickstart.py
